package com.cysion.study.ui.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.cysion.other.color
import com.cysion.study.R
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.darkTextTheme
import com.cysion.study.net.ApiClient
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import com.cysion.targetfun._subscribe
import kotlinx.android.synthetic.main.activity_pmanager.*
import kotlinx.android.synthetic.main.activity_pmanager.tb_onoff
import kotlinx.android.synthetic.main.activity_pmanager.topbar
import kotlinx.android.synthetic.main.fragment_p_teacher.*

class PManagerActivity : BaseActivity(), View.OnClickListener {
    override fun getLayoutId(): Int {
        return R.layout.activity_pmanager
    }

    override fun initView() {
        darkTextTheme(color(R.color.white))
        topbar.apply {
            setTitleBackColor(color(R.color.white))
            setTitle("家教管理")
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }
            }
        }

        ll_p_info.setOnClickListener(this)
        ll_p_manager.setOnClickListener(this)
        tb_onoff.setOnClickListener(this)
    }

    override fun onResume() {
        super.onResume()
        getUserInfos()
    }

    private fun getUserInfos() {
        ApiClient.api.getTeacherInfos(UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    if (!TextUtils.isEmpty(it.id)) {
                        UserCache.save(it)
                        UserCache.userInfo = it
                        /*userBean = it
                        updateTeacherStatus()*/

                        //ifJiajiao":"",//家教中是否招募学生：0、代表不招募；1、代表招聘
                        var ifJiajiao = UserCache.userInfo?.ifJiajiao
                        println("###家教页面开关="+ifJiajiao)
                        if (ifJiajiao == "0") {
                            tb_onoff.toggleOff()
                        } else if (ifJiajiao == "1") {
                            tb_onoff.toggleOn()
                        }

                        //个人介绍是否完善
                        //ifPersonInfo":"",//家教中个人信息介绍：0、代表未完善；1、代表已完善
                        var ifPersonInfo=UserCache.userInfo?.ifPersonInfo
                        if (ifPersonInfo == "0") {
                            tv_person.text = "未完善"
                        } else {
                            tv_person.text = "已填写"
                        }
                        var ifFmleduInfo=UserCache.userInfo?.ifFmleduInfo

                        //ifFmleduInfo":"",//家教信息是否完善：v
                        //家教信息是否完善
                        if (ifFmleduInfo == "0") {
                            tv_family.text = "未完善"
                        } else {
                            tv_family.text = "已填写"
                        }
                        println("####家教管理是否招募="+ifFmleduInfo)
                    }
                }
            }
    }

    override fun closeMvp() {
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tb_onoff -> {//招募
                if (tb_onoff.isToggleOn) {
                    setIfJiajiao("0")
                } else {
                    setIfJiajiao("1")
                }
            }
            R.id.ll_p_info -> {//个人信息
                var bundle = Bundle()
                bundle.putString("ifPersonInfo",UserCache.userInfo?.ifPersonInfo)
                UIHelper.jump(this,PInfoActivity::class.java,bundle)
            }
            R.id.ll_p_manager -> {//家教信息
                var bundle = Bundle()
                bundle.putString("ifFmleduInfo",UserCache.userInfo?.ifFmleduInfo)
                UIHelper.jump(this,PManagerInfoActivity::class.java,bundle)
            }
        }
    }

    private fun setIfJiajiao(ifJiajiao: String) {
        Alert.loading(self)
        //ifJiajiao":"",//家教中是否招募学生：0、代表不招募；1、代表招聘
        ApiClient.api.setIfJiajiao(ifJiajiao,UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    //家教中是否招募学生：0、代表关闭招募；1、代表开启招聘
                    if (ifJiajiao == "0") {
                        tb_onoff.toggleOff()
                    } else if (ifJiajiao == "1") {
                        tb_onoff.toggleOn()
                    }
                    UserCache.userInfo?.ifJiajiao=ifJiajiao!!
                    ShowToast("提交成功")

                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }
}
